package hu.bluerainbow.gifter.util;

import hu.bluerainbow.gifter.bean.Gift;
import hu.bluerainbow.gifter.filehandler.XlsHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class SteamChecker {

	public static void main(String[] args) {
		System.out.println("== Steam checker ==\n");
		
		
		
		String userName = null;
		String steamID = null;
		
		Scanner in = null;
		try {
			System.out.print("Blog username: " );
			in = new Scanner(System.in);
			// Reads a single line from the console
			// and stores into name variable
			userName = in.nextLine();
			
			System.out.print("SteamID: ");
			steamID = in.nextLine();
			
			System.out.println("\n\n");
		} finally {
			in.close();
		}
		
		XlsHandler xlsHandler = new XlsHandler(Configuration.getInputFileName());
		
		try {
			WebClient client = WebClientFactory.get();
			client.setJavaScriptEnabled(true);
			client.setAjaxController(new NicelyResynchronizingAjaxController());
			HtmlPage page = client.getPage("http://steamcommunity.com/profiles/" + steamID + "/games?tab=all");
			page = page.getElementById("all_pp").click();
			
			Set<String> steamGames = new TreeSet<String>();
			for (HtmlDivision div : (List<HtmlDivision>) page.getByXPath("//div[@class='gameListRowItemName ']")) {
				steamGames.add(div.getTextContent());
			}
			
			
			List<String> wonGames = new ArrayList<String>();
			
			for (Gift gift : xlsHandler.listWonSteamGames(userName)) {
				wonGames.add(gift.getName());
			}
			
	        List<String> unactivatedGames = new ArrayList<String>(wonGames);
	        unactivatedGames.removeAll(steamGames);
	        
	        System.out.println("\nPossibly unactivated games:");
	        for (String game : unactivatedGames) {
				System.out.println(" - " + game);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
